# coding:utf-8

import MySQLdb
import sys

'''
Mysql 测试
# http://www.codegood.com/archives/129 安装mysql-python
'''
# 设置下文件的编码方式
# sql查询中文有问题，设置下编码
reload(sys)
sys.setdefaultencoding('utf-8')


def operator():
    # 打开连接
    connection = MySQLdb.connect(host='10.180.29.19',
                                 user='mobapp',
                                 passwd='mobapp123',
                                 db='olicard',
                                 port=3306,
                                 charset='utf8'
                                 )
    # 获取操作游标
    cursor = connection.cursor()
    # 操作
    try:
        sql = "select provinceCode from olicard.sys_province where provinceName like '%%%%%s%%%%'" % '山东'
        sql = 'select provinceCode from sys_province where provinceName like \'%%%s%%\'' % '山东'
        # sql = "select provinceCode from olicard.sys_province where provinceCode like '%370%'"
        print sql
        cursor.execute(sql)
        # 获取一条数据
        data = cursor.fetchone()

        print data[0].encode('utf-8')
        print type(data[0].encode('utf-8'))

        # 事务控制
        connection.commit()

    except Exception as e:
        print e
        connection.rollback()
    finally:
        connection.close()


if __name__ == '__main__':
    operator()
